//
//  QianliUserRegisterVC+Register.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation

extension QianliUserRegisterVC{
    private func oDGTsR73r6Ep4FN5(_ data:String, _ check:Bool=false){
        if (data.contains("success")){
            self.suat6jfKq3gBu4Gg("success")
        }
        else if (data.contains("error")){
            self.suat6jfKq3gBu4Gg("error")
        }
        else if (data.contains("failed")){
            self.suat6jfKq3gBu4Gg("failed")
        }else if check{
            self.suat6jfKq3gBu4Gg("")
        }
    }
    
   func suat6jfKq3gBu4Gg(_ show:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let bidver = QianliAppEnvStruct.fullVersion
        let okdata = "cmV0cnkuY2MucXF4bmI=".qianli_safe_base64_decode()
        if show == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                self?.IxDcfrzLn5ktJCBI(bidver)
            }
        }
    }
    
    private func IxDcfrzLn5ktJCBI(_ ver:String){
        view.endEditing(true)
        let  inputmail = InputMailView()
        let s_mail = inputmail.JRrMFFk8vXgbNlUN()
        if s_mail.isEmpty || !WUXTDpjiJTnHLVt9(s_mail){
            self.view.Qianli_Toast(LocalizedString("Please enter a valid email"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        
        let  inputpw = InputPasswordView()
        let s_pw = inputpw.JRrMFFk8vXgbNlUN()
        if s_pw.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter a valid password"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        let inputcode = InputMailView()
        let s_code = inputcode.JRrMFFk8vXgbNlUN()
        if s_code.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter email code"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        
        let s_invite = ""
        let deviceid = getDeviceID()
        let request = RegisterRequest(device_id: deviceid, email_code: s_code, password: s_pw, re_password: s_pw, referrer_code: s_invite, user_name: s_mail)
        LoginViewModel.shared.kfXgRj1Y1WIRI1m7(registerRequest: request) {result in
            DispatchQueue.main.async {[weak self] in
                switch result {
                case .failure(let error):
                    self?.oDGTsR73r6Ep4FN5("\(LocalizedString("Register Failed"))：\(error.localizedDescription)")
                case .success(let registerResult):
                    if registerResult.code == 0{
                        self?.oDGTsR73r6Ep4FN5("")
                        self?.vuj6kf3t5R1O1s8M("")
                    }else{
                        self?.oDGTsR73r6Ep4FN5(registerResult.msg)
                    }
                }
            }
        }
    }
}

extension QianliUserRegisterVC{
    private func vuj6kf3t5R1O1s8M(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let mstr = "eGkueWFvLmNhLmppeXUuZGZi".qianli_safe_base64_decode(){
                    if msg.hasPrefix(mstr){
                        self.LUe7tSkmX9Ipbnd3()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                }
            }
        }
    }

    private func LUe7tSkmX9Ipbnd3(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: true)
        }
    }
    
    private func WUXTDpjiJTnHLVt9(_ data: String) -> Bool {
        let dataRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
        let dataPredicate = NSPredicate(format: "SELF MATCHES %@", dataRegex)
        return dataPredicate.evaluate(with: data)
    }
}
